<template>
  <page-header-wrapper>
    <page-tab-list></page-tab-list>
    <a-card :border="false">
      <a-alert style="margin:0 0 15px;" v-if="info"  :message="'定时任务规则：每十分钟访问URL：' + info" type="info" />
      <div class="tar">
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="importData(1)">导入用户 </a-menu-item>
            <a-menu-item key="2" @click="importData(2)">导入商品 </a-menu-item>
            <a-menu-item key="3" @click="importImg()">从银豹同步图片 </a-menu-item>
          </a-menu>
          <a-button> 导入 <a-icon type="down" /> </a-button>
        </a-dropdown>
      </div>

      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 3 }" :wrapper-col="{ span: 8 }">
        <a-form-model-item label="api_host" prop="api_host" help="例如：https://area11-win.pospal.cn:443">
          <a-input v-model="form.api_host" placeholder="请输入api_host" />
        </a-form-model-item>

        <a-form-model-item label="app_id" prop="app_id">
          <a-input v-model="form.app_id" placeholder="请输入app_id" />
        </a-form-model-item>

        <a-form-model-item label="app_key" prop="app_key">
          <a-input v-model="form.app_key" placeholder="请输入app_key" />
        </a-form-model-item>

        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="submitForm" :loading="modalLoading"> 提交 </a-button>
          <a-button style="margin-left: 10px" @click="$router.back(-1)"> 取消 </a-button>
          <a-button type="danger" style="margin-left: 10px" :loading="modalLoading" @click="remove">
            删除
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>

    <!-- 导入的弹窗 -->
    <a-modal v-model="visible_upload" :title="getImportTitle" @ok="handleUpload" :afterClose="closeModel_upload">
      <div style="padding: 10px">
        <span style="color: red">*</span><span class="m_l_5">EXCEL文件 : </span>
        <a-upload
          class="m_l_10"
          :file-list="fileList2"
          :remove="e => this.handleRemove(e, 'zip')"
          :before-upload="e => this.beforeUpload(e, 'zip')"
        >
          <a-button> <a-icon type="upload" /> 上传 </a-button>
        </a-upload>
      </div>
      <div style="color: red">{{ excelName }}</div>
      <div style="padding: 70px 10px 10px;text-align:center;">
        <a-button @click="downloadFile"> <a-icon type="download" /> 下载模板 </a-button>
      </div>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { conf, confSave, importProduct, importProductImg, importUser } from '@/api/yinbao'

export default {
  computed: {
    getImportTitle() {
      return `导入${this.importType === 1 ? '用户' : '商品'}`
    }
  },

  watch: {
    visible_upload(e) {
      if (!e) {
        this.fileList1 = []
        this.excelName = ''
      }
    }
  },

  data() {
    return {
      info: '',
      form: {
        api_host: '',
        app_id: '',
        app_key: ''
      },
      rules: {
        api_host: [{ required: true, message: '请输入api_host', trigger: 'blur' }],
        app_id: [{ required: true, message: '请输入app_id', trigger: 'blur' }],
        app_key: [{ required: true, message: '请输入app_key', trigger: 'blur' }]
      },
      modalLoading: false,
      visible_upload: false,
      importType: 1,
      fileList1: [],
      excelName: '',
    }
  },

  methods: {
    importData(type) {
      this.importType = type
      this.visible_upload = true
    },
    importImg() {
      importProductImg().then(res => {
        if (res.code === 0) {
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      });
    },

    beforeUpload(file) {
      console.log(file)
      this.fileList1 = [file]
      this.excelName = file.name
      return false
    },
    handleUpload() {
      const formData = new FormData()
      formData.append('excel', this.fileList1[0])
      console.log(formData)
      this.uploading = true

      if (this.importType === 1) {
        importUser(formData)
          .then(res => {
            this.uploading = false

            if (res.code === 0) {
              this.$message.success(res.msg)
              this.fileList1 = []
              this.visible_upload = false
              this.excelName = ''
            } else {
              this.$message.error(res.msg)
            }
          })
          .catch(() => {
            this.uploading = false
          })
        return
      }

      importProduct(formData)
        .then(res => {
          this.uploading = false

          if (res.code === 0) {
            this.$message.success(res.msg)
            this.fileList1 = []
            this.visible_upload = false
            this.excelName = ''
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.uploading = false
        })
    },
    downloadFile() {
      let url = '/web/statics/yinbaoproduct.xlsx'
      if (this.importType === 1) {
        url = '/web/statics/yinbaouser.xlsx'
      }
      window.open(url)
    },

    remove() {
      this.$confirm({
        title: '提示！',
        content: '是否删除配置？',
        onOk: () => {
          this.confSave({
            api_host: '',
            app_id: '',
            app_key: ''
          })
        },
        onCancel: () => {}
      })
    },

    confSave(conf) {
      this.modalLoading = true

      confSave({
        conf
      })
        .then(res => {
          this.modalLoading = false

          if (res.code !== 0) {
            this.$error({
              content: res.msg
            })
          } else {
            this.getData()
            this.$message.success(res.msg)
            this.visible = false
          }
        })
        .catch(err => {
          this.$message.error(err.message)
          this.modalLoading = false
        })
    },

    // 表单提交
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let { form } = this

          this.confSave(form)
        }
      })
    },

    getData() {
      conf().then(res => {
        if (res.code === 0) {
          let { api_host, app_key, app_id } = res.data

          this.form = {
            app_id,
            api_host,
            app_key
          }

          this.info = res.crontabLink;
        }
      })
    }
  },

  mounted() {
    this.getData()
  }
}
</script>

<style lang="less" scoped>
.title {
  color: #000;
  font-size: 16px;
  margin-bottom: 10px;
}
.tip {
  font-size: 14px;
  color: #8c8c8c;
  display: block;
}
</style>
